package com.productivity.model.animation;

import java.io.Serializable;

import com.productivity.model.common.Vertex3D;
import com.productivity.view.MyCanvas;

public abstract class Animation implements Serializable {
	private static final long serialVersionUID = 5596853036838464041L;
	protected int currFrame;
	protected int nextFrame;
	protected Vertex3D[][] frames;
	protected double duration;
	protected boolean repeats;
	protected float fracComplete;
	protected AnimationType type;
	protected AnimationListener listener;
	protected boolean finish;
	
	private float[] verts = {0.00000f, -0.00000f, -0.07840f, -0.74589f, -0.07840f, -0.74589f, 0.03893f, -1.48666f, 0.00000f, -0.00000f, -0.07840f, -0.74589f, -0.07840f, -0.74589f, 0.03893f, -1.48666f, 0.00000f, -0.00000f, 0.00000f, -1.00000f, 0.00000f, -1.00000f, -0.01745f, -1.99985f, -0.01745f, -1.99985f, -0.01745f, -2.99985f, 0.00000f, -1.00000f, 0.01745f, -1.99985f, 0.01745f, -1.99985f, 0.01745f, -2.99985f};
	
	
	public Animation(AnimationListener listener) {
		frames = new Vertex3D[0][0];
		nextFrame=1;
		duration = 0;
		type = AnimationType.STAND;
		repeats = false;
		finish = false;
		this.listener = listener;
	}
	public void update() {
		fracComplete+=frames.length/(duration*MyCanvas.FPS);
		if(fracComplete>=1) {
			currFrame=nextFrame;
			nextFrame++;
			fracComplete = 0;
			if(nextFrame >= frames.length || finish) {
				if(repeats)
					nextFrame = 0;
				else if(listener!=null)
					listener.animationFinished(this.type);
			}
		}
	}
	public Vertex3D[] getInterFrame() {
		if(nextFrame >= frames.length) return frames[frames.length-1];
		Vertex3D frame[] = new Vertex3D[frames[0].length];
		if(!finish) {
			for(int i = 0; i < frame.length; i++) {
				frame[i] = new Vertex3D(frames[currFrame][i], frames[nextFrame][i], fracComplete);
			}
		} else {
			for(int i = 0; i < frame.length; i++) {
				frame[i] = new Vertex3D(frames[currFrame][i], new Vertex3D(verts[i*2],verts[i*2+1],verts[i*2+2]), fracComplete);
			}
		}
		return frame;
	}
	public void stopAnimation() {
		finish = true;
		fracComplete=0;
		repeats = false;
	}
}
